package net.towee.model.group.impl;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import net.towee.model.account.Role;
import net.towee.model.group.GroupData;
import net.towee.model.group.GroupEnterRequest;
import net.towee.model.group.GroupIdentity;
import net.towee.model.group.GroupOpenness;
import net.towee.model.group.GroupProfile;
import net.towee.model.group.RoleContainer;
import net.towee.model.impl.ProfileContainerImpl;
import net.towee.server.persistence.impl.MongoDbStore;

import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
import com.google.code.morphia.annotations.Indexed;
import com.google.code.morphia.annotations.PostLoad;
import com.google.code.morphia.annotations.Reference;

/**
 * Implementation of group data
 * @author c58
 */
@Entity(MongoDbStore.GROUP_COLLECTION)
public class GroupDataImpl extends ProfileContainerImpl implements GroupData, RoleContainer {
	@Id private String id;
	
	// Referenced identity
	@Indexed @Reference private GroupIdentity identity;
	
	// Test question
	private String testQuest;
	@Indexed private String testAnsw;
	
	// Profile of group
	@Reference private GroupProfile profile;
	
	// Roles
	@Reference private List<Role> roles;
	@Reference private Role defaultRole;
	
	// Enter requests
	private List<GroupEnterRequest> requests;

	public GroupDataImpl(){}
	public GroupDataImpl(GroupIdentity identity, GroupOpenness openness){
		this.id = identity.getId();
		this.identity = identity;

		this.roles = new LinkedList<Role>();
		this.profile = new GroupProfileImpl(identity, openness);
		this.requests = new ArrayList<GroupEnterRequest>();
	}
	
	@PostLoad
	public void createEmptyFileds() {
		if(roles == null)
			this.roles = new LinkedList<Role>();
		if(requests == null)
			this.requests = new ArrayList<GroupEnterRequest>();
	}
	
	@Override
	public String getId() {
		return id;
	}

	@Override
	public GroupIdentity getIdentity() {
		return identity;
	}

	@Override
	public RoleContainer getRoleContainer() {
		return this;
	}

	@Override
	public GroupProfile getProfile() {
		return profile;
	}

	@Override
	public void setTestQuestion(String quest, String answer) {
		this.testQuest = quest;
		this.testAnsw = answer;
	}
	
	/*
	 * Roles container
	 */
	@Override
	public void addRole(Role role) {
		this.roles.add(role);
	}

	@Override
	public void deleteRole(Role role) {
		this.roles.remove(role);
	}

	@Override
	public void deleteRole(int index) {
		if(index < this.roles.size())
			this.roles.remove(index);
	}

	@Override
	public int getRoleIndex(Role role) {
		return this.roles.indexOf(role);
	}

	@Override
	public Role getRole(int index) {
		if(index < this.roles.size())
			return this.roles.get(index);
		
		return null;
	}

	@Override
	public List<Role> getRoleList() {
		return roles;
	}

	@Override
	public Role getDefaultRole() {
		return defaultRole;
	}

	@Override
	public void setDefaultRole(Role role) {
		this.defaultRole = role;
	}
	@Override
	public String getTestQuestion() {
		return this.testQuest;
	}
	@Override
	public List<GroupEnterRequest> getEnterRequests() {
		return this.requests;
	}
	@Override
	public String getTestAnswer() {
		return this.testAnsw;
	}
	@Override
	public void setId(String string) {
		this.id = string;
		this.identity.setId(string);
		this.profile.setId(string);
	}

}
